package stocks.client.gui.controller.actions;

import stocks.client.gui.model.model;
import stocks.server.CallBack;

import java.awt.event.ActionEvent;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JTable;

public class GetTransactionsAction extends AbstractAction{

	private model m;
	
	public GetTransactionsAction(model m)
	{
		this.m = m;
	}
	@Override
	public void actionPerformed(ActionEvent e) {

		String startDate = m.getViewFrame().getStartDate();
		String endDate = m.getViewFrame().getEndDate();
		ArrayList<String> al = new ArrayList<String>();
		
		CallBack c = null;
		try {
			al = m.getRMIInterface().requestTransactions(c, startDate, endDate);
			System.out.println("Got transaction history");

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JTable results = m.getViewFrame().getResultsTable();
		int i = 0;
		int row = 0;
		int column = 0;
		int k = 0;
		while(al.size() > i) //set value of table from arraylist
		{
			results.setValueAt(al.get(i), row, column);
			column++;
			k++;
			if(k == 5)
			{
				row++;
				column = 0;
				k=0;
			}
			i++;
		}
	}
		
}
